<template>
    <div>
        <el-container>
            <el-main>
                <h1>用户注册</h1>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                  <el-input type="text" maxlength="12" v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="confirmPassword">
                  <el-input type="password" v-model="ruleForm.confirmPassword" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号码" prop="phonenumber">
                  <el-input type="text" v-model="ruleForm.phonenumber"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button @click="registerFromForm('ruleForm')">注册</el-button>
                </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
</template>
<script>
export default {
    name: 'Register',
    data() {
        var validatePhone = (rule, value, callback) => {
            const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
            if (value === '') {
                callback();
            } else {
                const isPhone = reg.test(value);
                value = Number(value);
                if (isPhone && typeof value === "number" && !isNaN(value) && value.toString().length == 11) {
                    callback();
                } else {
                    callback('请输入正确的 11 位手机号！');
                }
            }
        };
        var validatePassword = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.password) {
                callback('两次输入密码不一致！');
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                username: '',
                password: '',
                phonenumber: '',
            },
            rules: {
                password: [
                    {required: true, message: '请输入密码' },
                    {min: 8, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur'},
                ],
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur'},
                    {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'}
                ],
                confirmPassword: [
                    { validator: validatePassword, trigger: 'blur'},
                    { required: true, message: '请再次输入密码', trigger: 'blur'},
                ],
                phonenumber: [
                    { validator: validatePhone, trigger: 'blur'},
                ],
            }
        }
    },
    methods: {
        registerFromForm(formName) {
            const _this = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$axios.post('/user/register', this.ruleForm).then((res) => {
                        // const token = res.headers['authorization'] // 从返回的结果中获取到 token 信息
                        this.$notify({
                          title: '注册成功',
                          message: '即将跳转到首页……',
                          type: 'success'
                        });
                        _this.$router.push('/login')
                    }).catch(function (error) {
                        console.debug(error);
                        _this.$notify.error({
                            title: '注册失败！',
                            message: '具体错误 ' + error
                        })
                    })
                } else {
                    return false;
                }
            });
        }
    }
}
</script>